package org.game.manager;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sojoys.artifact.manager.PropManager;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import com.xiaoleilu.hutool.util.ClassUtil;
import org.cfg.cache.DiscreteDataCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.game.constant.GameConstant;
import org.game.db.bean.CurrentScriptRecord;
import org.game.db.bean.Dating;
import org.game.db.bean.Player;
import org.game.module.datingbo.DatingType;
import org.game.module.datingbo.handler.DatingHandler;
import org.game.protobuf.c2s.C2SDatingMsg;

import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 约会管理器 Created by fxf on 2017/8/15.
 */
@SuppressWarnings("unchecked")
public class DatingManager {

    static Log log = LogFactory.get(DatingManager.class);

    private static Map<Integer, DatingHandler> datingHandlerMap = Maps.newHashMap();

    static {
        datingHandlerMap.clear();
        log.debug("----------------------------Scan Dating Handler Begin----------------------------");
        Set<Class<?>> scanClasses = ClassUtil.scanPackageBySuper(PropManager.get(GameConstant.SCAN_DATING_HANDLER), DatingHandler.class);
        for (Class scanClass : scanClasses) {
            if (scanClass.isInterface()) {
                continue;
            }
            if (Modifier.isAbstract(scanClass.getModifiers())) {
                continue;
            }
            Object handler = ClassUtil.newInstance(scanClass);
            DatingType annotation = (DatingType) scanClass.getAnnotation(DatingType.class);
            int eventType = annotation.value();
            datingHandlerMap.put(eventType, (DatingHandler) handler);
            log.debug("Registry DatingHandler：{}", handler.getClass().getName());
        }
        log.debug("----------------------------Scan Dating Handler End----------------------------");
    }

    /**
     * 获取剧本
     */
    public static void getScript(Player player, int datingType, C2SDatingMsg.GetScriptMsg msg) {
        datingHandlerMap.get(datingType).getScript(player, msg);
    }

    /**
     * 对话
     */
    public static byte[] dialog(Player player, C2SDatingMsg.DialogueMsg msg) {
        return datingHandlerMap.get(msg.getDatingType()).dialog(player, msg);
    }

    /**
     * 初始化约会记录对象
     *
     * @param player 玩家对象
     */
    public static Dating getDating(Player player) {
        Dating dating = player.getDatingsFkPlayerId();
        if (dating == null) {
            dating = new Dating(0L, player.getId(), Maps.newHashMap(), Lists.newArrayList(), Lists.newArrayList(), new Date(), new Date());
            dating.insert();
        }
        return dating;
    }

    public static DatingHandler getDatingHandlerByType(int datingType){
        return datingHandlerMap.get(datingType);
    }

    /**
     * 删除临时约会记录
     * @param player
     */
    public static void dealTempDating(Player player){
        List<CurrentScriptRecord> list = player.getCurrentScriptRecordsFkPlayerId();
        List<Integer> notReentryDatingTypeList = (List<Integer>) DiscreteDataCfgCache.me().getData(DiscreteDataID.DATING, DiscreteDataKey.REENTRY_TYPE);
        if (notReentryDatingTypeList == null || notReentryDatingTypeList.size() ==0){
            return;
        }
        for (CurrentScriptRecord scriptRecord : list) {
            if (notReentryDatingTypeList.contains(scriptRecord.getDatingType())){
                scriptRecord.delete();
            }
        }
    }
}
